<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box flex flex-center">
                <el-button type="primary" icon="el-icon-folder" @click="submitForm('ruleForm')">保存</el-button>
            </div>
            <div class="table-form-box">
                <h3 class="h3">转正申请</h3>
                <el-form :model="formData" :rules="rules" ref="ruleForm">
                    <table border="0" width="100%" cellpadding="0" cellspacing="1">
                        <tbody>
                            <tr class="tr-title">
                                <td colspan="6">基本信息</td>
                            </tr>
                            <tr>
                                <td align="right">单据编号:</td>
                                <td>
                                    <el-form-item prop="recordNo">
                                        <el-input
                                            placeholder="保存后自动生成"
                                            v-model="formData.recordNo"
                                            size="small"
                                            disabled
                                        ></el-input>
                                    </el-form-item>
                                </td>
                                <td align="right">
                                    <span class="color-ff2200">*</span>姓名:
                                </td>
                                <td>
                                    <el-form-item prop="correctUserName">
                                        <div @click="showTreeDialog('姓名')">
                                            <!--puperiorUserId-->
                                            <el-input
                                                placeholder="请选择"
                                                v-model="formData.correctUserName"
                                                size="small"
                                                readonly
                                                suffix-icon="el-icon-search"
                                            ></el-input>
                                        </div>
                                    </el-form-item>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">部门:</td>
                                <td>
                                    <el-form-item prop="departName">
                                        <el-input
                                            v-model="formData.departName"
                                            size="small"
                                            disabled
                                        ></el-input>
                                    </el-form-item>
                                </td>
                                <td align="right">岗位:</td>
                                <td>
                                    <el-form-item prop="jobRole">
                                        <el-input v-model="formData.jobRole" size="small" disabled></el-input>
                                    </el-form-item>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <span class="color-ff2200">*</span>入职日期:
                                </td>
                                <td>
                                    <el-form-item prop="onbordingTime">
                                        <el-date-picker
                                            style="width:99%"
                                            v-model="formData.onbordingTime"
                                            align="right"
                                            type="date"
                                            placeholder="选择日期"
                                        ></el-date-picker>
                                    </el-form-item>
                                </td>

                                <td align="right">
                                    <span class="color-ff2200">*</span>申请时间:
                                </td>
                                <td>
                                    <el-form-item prop="applyTime">
                                        <el-date-picker
                                            style="width:99%"
                                            v-model="formData.applyTime"
                                            align="right"
                                            type="date"
                                            placeholder="选择日期"
                                        ></el-date-picker>
                                    </el-form-item>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <span class="color-ff2200">*</span>申请转正日期:
                                </td>
                                <td>
                                    <el-form-item prop="epplyCorrentTime">
                                        <el-date-picker
                                            style="width:99%"
                                            v-model="formData.epplyCorrentTime"
                                            align="right"
                                            type="date"
                                            placeholder="选择日期"
                                        ></el-date-picker>
                                    </el-form-item>
                                </td>
                                <td align="right"></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td align="right">备注:</td>
                                <td colspan="5">
                                    <el-input
                                        placeholder="请输入内容"
                                        type="textarea"
                                        :rows="3"
                                        v-model="formData.remarks"
                                        clearable
                                    ></el-input>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </el-form>
                <upload-files :files="fileList" @fun="dealFiles" v-if="flag"></upload-files>
            </div>
        </div>
        <tree-search
            :isShow="showSearch"
            :dialogTitle="searchDialogTit"
            @edit="searchBack"
            @close="closeTreeDialog"
        ></tree-search>
    </div>
</template>

<script>
import appMain from '../../../utils/app_main';
export default {
    name: 'basetable',
    data() {
        return {
            pageTitle: '',
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            formData: {
                applyTime: '', // 申请时间
                correctUserId: '', // 转正申请人id
                correctUserName: '', // 转正申请人
                departName: '', // 部门名称
                departId: '', // 部门编号
                epplyCorrentTime: '', // 申请转正时间
                jobRole: '', // 所属岗位
                jobRoleId: '', // 所属岗位ID
                onbordingTime: '', // 入职日期
                recordNo: '', // 单据编号
                remarks: '', // 备注信息
                status: '', // 流程状态
                statusTypeId: '', // 表单类型ID
                statusTypeName: '', // 表单类型名称

                uploadReturns: [
                    // 附件上传详情
                    {
                        fileName: '', // 文件名
                        fileType: '', // 文件类型名称
                        fileUrl: '', // 文件所在地址
                        ownerId: '', // 所关联的表主键id.即是该文件所属的表的主键
                        uploadUserId: '', // 文件上传人员ID
                        uploadUserName: '' //  文件上传人员名字
                    }
                ]
            },
            rules: {
                //合同名称
                correctUserName: [
                    {
                        required: true,
                        message: '请选择',
                        trigger: 'change'
                    }
                ],
                //入职日期
                onbordingTime: [
                    {
                        required: true,
                        message: '请选择',
                        trigger: 'change'
                    }
                ],
                //申请时间
                applyTime: [
                    {
                        required: true,
                        message: '请选择',
                        trigger: 'change'
                    }
                ],
                //申请转正日期
                epplyCorrentTime: [
                    {
                        required: true,
                        message: '请选择',
                        trigger: 'change'
                    }
                ]
            },
            flag: false,
            isEdit: false,
            editId: '',
            dialogVisible: false,

            // 上传文件,选择的文件
            fileList: [],

            showSearch: false, // 显示搜索树弹窗
            searchDialogTit: '编辑',
            departList: []
        };
    },
    created() {
        // 如果有id就是跳转过来修改的
        if (this.$route.query.id) {
            this.editId = this.$route.query.id;
            this.isEdit = true;
            this.getData(); // 获取要修改的数据
        } else {
            this.isEdit = false;
            this.flag = true;
        }
    },
    methods: {
        getData() {
            appMain
                .correntFindById({}, this.editId)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.formData = res.data.data;
                        //获取人员档案附件
                        this.uploadFileInfoFindById(this.formData.id);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        //根据id查询附件
        uploadFileInfoFindById(id) {
            appMain
                .uploadFileInfoFindById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.fileList = res.data.data;
                        this.flag = true;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        submit() {
            let params = this.formData;
            let mothedName = '';
            if (this.isEdit) {
                mothedName = 'correntUpdateById';
            } else {
                mothedName = 'correntInsert';
            }

            appMain[mothedName](params)
                .then(res => {
                    if (res.data.code === 20000) {
                        if (this.isEdit) {
                            this.getData();
                        }
                        this.$router.push({
                            //核心语句
                            path: '/applicationList' //跳转的路径
                        });
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    } else {
                        this.$message({
                            message: res.data.message,
                            type: 'error'
                        });
                    }
                })
                .catch(err => {
                    console.log(err);
                });
            console.log(this.$router);
        },
        // 选择员工
        showTreeDialog(tit) {
            console.log('showTreeDialog');
            this.searchDialogTit = tit;
            this.showSearch = true;
        },

        // 树组件搜索结果返回
        searchBack(data) {
            console.log(data.backData, 'searchTreeBack');
            this.formData.correctUserName = data.backData.name;
            this.formData.correctUserId = data.backData.id;
            this.formData.jobRole = data.backData.jobName;
            this.formData.jobRoleId = data.backData.jobId;
            this.formData.departName = data.backData.organizationName;
            this.formData.departId = data.backData.organizationId;
            this.showSearch = false;
        },
        closeTreeDialog(data) {
            this.showSearch = data.showDialog;
        },
        // 上传文件返回
        dealFiles(data) {
            this.fileList = data.backData;
            this.formData.uploadReturns = this.fileList;
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.submit();
                    //this.resetForm('ruleForm');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    },
    mounted() {
        this.pageTitle = this.$router.currentRoute.meta.title;
    }
};
</script>

<style scoped>
.container {
    padding-bottom: 50px;
}
.handle-box {
    /* padding: 5px 50px; */
    width: 100%;
    background-color: #fff;
    position: absolute;
    left: 0;
    bottom: 0px;
    border-bottom: 1px solid #ddd;
    border-top: 1px solid #ddd;
    z-index: 1;
}

.table {
    width: 100%;
    font-size: 14px;
}

.table-form-box table {
    background-color: #666;
}
.table-form-box .h3 {
    font-size: 22px;
    text-align: center;
    font-weight: normal;
    padding: 20px 0;
}
.table-form-box table .tr-title td {
    font-size: 14px;
    line-height: 40px;
}
.table-form-box table td,
.table-form-box table th {
    background-color: #fff;
    padding: 4px;
    min-height: 40px;
    font-size: 12px;
}
.color-ff2200 {
    color: #ff2200;
}
</style>
